package com.atguigu.cloud.sevice.Impl;/**
 * @Auter zzh
 * @Date 2024/11/13
 */

import cn.hutool.db.PageResult;
import com.atguigu.cloud.entities.*;
import com.atguigu.cloud.mapper.OrderMapper;
import com.atguigu.cloud.mapper.ProductMapper;
import com.atguigu.cloud.mapper.ShopcarMapper;
import com.atguigu.cloud.sevice.UserService;
import com.atguigu.cloud.tool.load.page.PaginationResult;
import com.atguigu.cloud.tool.load.page.QueryObject.BaseQueryObject;
import com.atguigu.cloud.tool.load.page.QueryObject.QueryObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import com.atguigu.cloud.mapper.UserMapper;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import com.atguigu.cloud.tool.getdata;


import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.sevice.Impl
 * @className: userServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/13 18:02
 * @version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    ShopcarMapper ShopcarMapper;
    @Resource
    ProductMapper productMapper;
    @Resource
    ShopcarMapper shopcarMapper;
    @Resource
    OrderMapper orderMapper;
    @Override
    public User getById(Integer id)
    {

        QueryWrapper<User> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user= userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public List<User> getAll()
    {
        QueryWrapper<User> queryWrapper= new QueryWrapper<>();
        List<User> list_user = userMapper.selectList(queryWrapper);
        return list_user;
    }

    @Override
    public int add(User user)
    {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public int delete(Integer id)
    {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int result_id =userMapper.delete(queryWrapper);
        return result_id;
    }

    @Override
    public int update(User user)
    {
        int id = user.getId();
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        userMapper.update(user,updateWrapper);
        return id;
    }
    //用户查询购物车里的商品
    @Override
    public List<Map<String, Object>> getProductList(Integer id) {
        QueryWrapper<Shopcar> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("products")
                .eq("userid", id);
        List<Map<String, Object>> list_product = ShopcarMapper.selectMaps(queryWrapper);
        return list_product;
    }
    //用户删除购物车里的商品
    @Override
    public int delProduct(Integer id, Integer products) {
        QueryWrapper<Shopcar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", id)
                .eq("products", products);
        int result_id = ShopcarMapper.delete(queryWrapper);
        return result_id;
    }
    //用户下单支付创建订单

    //这里是不是有点问题
    @Override
    public String  createOrder(Map<String, Integer> map) {
        var data=getdata.getdate();
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("inventory","price")
                .eq("proid", map.get("products"));
        Product product = productMapper.selectOne(queryWrapper);

        if (product==null || product.getInventory() <= 0)
        {
            return "proid有问题或者库存不足";
        }
        Order order = new Order(null, map.get("userid"), data, product.getPrice(), "1", map.get("products").toString());
        int i=orderMapper.insert(order);
        if (this.delProduct(map.get("userid"), map.get("products"))==0)
        {
            return "用户的购物车里没有这个商品";
        }
        return i+"";
    }

    @Override
    public boolean onlyName(String name) {
        QueryWrapper<User> querywrapper = new QueryWrapper<>();
        querywrapper.eq("name",name);
        return userMapper.selectList(querywrapper).isEmpty();
    }

    @Override
    public Integer getIdByName(String name) {
        QueryWrapper<User> querywrapper = new QueryWrapper<>();
        querywrapper.eq("name",name).select("id");
        return  userMapper.selectOne(querywrapper).getId();
    }


    //分页查询实现
    @Override
    public PaginationResult<User> myPaginationQuery(Integer currentPage) {
            //为0排除
            if (currentPage==0){
                throw new RuntimeException("输入目标页号为0,这将不被允许");
            }
            //分页对象,包含分页大小和分页号数
            BaseQueryObject qo =new BaseQueryObject(currentPage);

            QueryWrapper<User> queryWrapper=new QueryWrapper<>();

            // 获取查询的记录数
            long totalCount = userMapper.selectCount(queryWrapper);
            // 获得查询的目标结果集,(这里就用基础的mybatis实现了)
            List<User> list_user = userMapper.PaginationQuery(qo.getStart(),qo.getPageSize());

            // 如果总记录数为0，那么说明没数据就不用下一步查询了，提高效率。
            if (totalCount == 0){
                // 返回一个查询结果集，返回当前页、每页记录数、以及一个空的结果集
                return new PaginationResult<User>(qo.getCurrentPage(),qo.getPageSize(),totalCount, Collections.EMPTY_LIST);
            }

            // 返回一个查询结果集，返回当前页、每页记录数、以及结果集
            return new PaginationResult<User>(qo.getCurrentPage(),qo.getPageSize(),totalCount,list_user);

        }


}
